package com.ken.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ken.dto.OrdersPageQueryDTO;
import com.ken.dto.OrdersSubmitDTO;
import com.ken.entity.Orders;
import com.ken.result.PageResult;
import com.ken.result.Result;
import com.ken.service.OrderDetailService;
import com.ken.service.OrderService;
import com.ken.vo.OrderSubmitVO;
import com.ken.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags ="C端-用户订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> queryHistoryOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("接收到分页查询参数：{}",ordersPageQueryDTO);
        Page<Orders> page = orderService
                .query()
                .like(ordersPageQueryDTO.getStatus() != null, "status", ordersPageQueryDTO.getStatus())
                .page(new Page<>(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize()));
        return Result.success(new PageResult(page.getTotal(), page.getRecords()));
    }

    @GetMapping("/orderDetail/{id}")
    @ApiOperation("根据id查询订单详情")
    public Result<OrderVO> findOrdersDetailById(@PathVariable Long id){
        log.info("查询id为{}的订单",id);
        OrderVO orderVo = orderService.findById(id);
        return Result.success(orderVo);
    }
    @GetMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> userSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<String> repetitionById(@PathVariable("id")Long id){
        orderService.repetitionOrderById(id);
        return Result.success();
    }


    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancel(@PathVariable Long id) throws Exception {
        log.info("取消的订单id为:{}",id);
        orderService.cancelOrderById(id);
        return Result.success();
    }

    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result<String> reminder(@PathVariable Long id){
        orderService.reminderById(id);
        return Result.success();
    }


}
